﻿package mortal.game.view.shop
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.shopMall.view.*;
    import mortal.mvc.core.*;

    public class BuyAdvanceGiftPart extends GSprite
    {
        private var _tShopPanicBuyItem:TShopPanicBuyItem;
        private var _shopItem:BaseItem;
        private var _propName:GTextFiled;
        private var _disCountTxt:GTextFiled;
        private var _oldPrice:GTextFiled;
        private var _price:GTextFiled;
        private var _leftNum:GTextFiled;
        private var _numericStepper:GNumericStepper;
        private var _buyBtn:GLoadingButton;
        private var _priceIcon1:GBitmap;
        private var _priceIcon2:GBitmap;

        public function BuyAdvanceGiftPart()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._shopItem = UICompomentPool.getUICompoment(ShopSellItem);
            this._shopItem.x = 11;
            this._shopItem.y = 33;
            this._shopItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            this.addChild(this._shopItem);
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 15;
            _loc_1.bold = true;
            this._propName = UIFactory.gTextField("", 11, 4, 235, 25, this, _loc_1, true);
            _loc_1.size = 12;
            _loc_1.bold = false;
            this._oldPrice = UIFactory.gTextField("", 109, 32, 140, 20, this, GlobalStyle.textFormatlan);
            this._price = UIFactory.gTextField("", 109, 56, 140, 20, this, _loc_1);
            _loc_1.align = TextFormatAlign.RIGHT;
            this._numericStepper = UIFactory.gNumericStepper(91, 82, 50, 20, this, 99, 1, "NumericStepper", _loc_1);
            this._numericStepper.configEventListener(Event.CHANGE, this.updateNum);
            this._numericStepper.value = 1;
            _loc_1 = GlobalStyle.textFormatHuang2;
            _loc_1.size = 17;
            _loc_1.bold = true;
            this._disCountTxt = UIFactory.gTextField("", 160, 2, 80, 25, this, _loc_1);
            _loc_1.align = TextFormatAlign.CENTER;
            _loc_1.size = 12;
            this._leftNum = UIFactory.gTextField("", 5, 113, 230, 25, this, _loc_1);
            this._buyBtn = UIFactory.gLoadingButton(ResFileConst.ShopMallBuyBtn, 175, 81, 51, 24, this);
            this._buyBtn.configEventListener(MouseEvent.CLICK, this.buyItemHandler);
            this._priceIcon1 = UIFactory.gBitmap("", 200, 32, this);
            this._priceIcon2 = UIFactory.gBitmap("", 200, 56, this);
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.deleteLine, 167, 39, this));
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateNum);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateNum);
            this._shopItem.dispose(param1);
            this._shopItem = null;
            this._propName.dispose(param1);
            this._propName = null;
            this._numericStepper.dispose(param1);
            this._numericStepper = null;
            this._buyBtn.dispose(param1);
            this._buyBtn = null;
            this._priceIcon1.dispose(param1);
            this._priceIcon1 = null;
            this._priceIcon2.dispose(param1);
            this._priceIcon2 = null;
            this._disCountTxt.dispose(param1);
            this._disCountTxt = null;
            this._oldPrice.dispose(param1);
            this._oldPrice = null;
            this._price.dispose(param1);
            this._price = null;
            this._leftNum.dispose(param1);
            this._leftNum = null;
            this._tShopPanicBuyItem = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function updateNum(param1:Object = null) : void
        {
            var _loc_2:RebateItemData = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:GTextFormat = null;
            if (this._tShopPanicBuyItem)
            {
                _loc_2 = Cache.instance.shop.getRebateInfoDicByItemCode(this._tShopPanicBuyItem.itemCode);
                if (_loc_2 && _loc_2.remainInfo)
                {
                    _loc_3 = _loc_2.remainInfo.discount * this._numericStepper.value;
                    _loc_4 = this._tShopPanicBuyItem.price * this._numericStepper.value;
                    this._oldPrice.text = Language.getString(35528) + _loc_4;
                    if (Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, _loc_3, null, false))
                    {
                        _loc_5 = GlobalStyle.textFormatBai;
                    }
                    else
                    {
                        _loc_5 = GlobalStyle.textFormatHong;
                    }
                    this._price.defaultTextFormat = _loc_5;
                    this._price.text = Language.getString(35529) + _loc_3;
                }
            }
            return;
        }// end function

        public function buyItemHandler(event:MouseEvent = null) : void
        {
            var _loc_3:Array = null;
            var _loc_6:Array = null;
            var _loc_7:Array = null;
            var _loc_8:ItemInfo = null;
            var _loc_9:int = 0;
            var _loc_2:* = BuyGiftItemWin.isNeedTobuyCurTime ? (this._numericStepper.value) : (0);
            var _loc_4:* = this._shopItem.itemData;
            if (_loc_4)
            {
                if (ItemsUtil.isMulSelBag(_loc_4) && BuyGiftItemWin.isViewShow)
                {
                    _loc_6 = (_loc_4.itemInfo as ItemGiftBagInfo).itemList.split("&");
                    while (_loc_9 < _loc_6.length)
                    {
                        
                        _loc_7 = (_loc_6[_loc_9] as String).split("#");
                        _loc_8 = ItemConfig.instance.getConfig(int(_loc_7[1]));
                        if (_loc_8.type == BuyGiftItemWin.targetItemData.itemInfo.type)
                        {
                            _loc_3 = [int(_loc_7[3])];
                            break;
                        }
                        _loc_9++;
                    }
                }
            }
            var _loc_5:Object = {item:this._shopItem, num:this._numericStepper.value, useNum:_loc_2, arr:_loc_3};
            Dispatcher.dispatchEvent(new DataEvent(EventName.RebateShopBuyItem, _loc_5));
            return;
        }// end function

        public function set data(param1:TShopPanicBuyItem) : void
        {
            this._tShopPanicBuyItem = param1;
            this._shopItem.itemData = new ItemData(this._tShopPanicBuyItem.itemCode);
            this._propName.htmlText = ItemsUtil.getItemName(this._shopItem.itemData);
            var _loc_2:* = Math.round(this._shopItem.itemData.itemInfo.effect / this._shopItem.itemData.itemInfo.effectEx * 100);
            this._disCountTxt.text = "[ " + (_loc_2 * 0.1).toString().slice(0, 3) + Language.getString(35530);
            this._priceIcon1.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(EPriceUnit._EPriceUnitGold));
            this._priceIcon2.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(EPriceUnit._EPriceUnitGold));
            this._oldPrice.text = Language.getString(35528) + this._tShopPanicBuyItem.price;
            this._numericStepper.maximum = this._tShopPanicBuyItem.buyLimit > 0 ? (this._tShopPanicBuyItem.buyLimit) : (99);
            var _loc_3:* = Cache.instance.shop.getRebateInfoDicByItemCode(this._tShopPanicBuyItem.itemCode);
            if (_loc_3 && _loc_3.remainInfo)
            {
                this._price.text = Language.getString(35529) + _loc_3.remainInfo.discount;
                if (_loc_3.remainInfo.leftAmount >= 0)
                {
                    this._leftNum.text = Language.getStringByParam(35531, _loc_3.remainInfo.leftAmount.toString());
                }
                else
                {
                    this._leftNum.text = "";
                }
            }
            this.updateNum();
            return;
        }// end function

    }
}
